package com.siwei.mes.service.experiment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.util.ScriptUtils;
import com.siwei.mes.entity.experiment.CheckQuickConfig;
import com.siwei.mes.mapper.experiment.CheckQuickConfigMapper;
import com.siwei.mes.mapper.experiment.ExperimentCheckItemorderMapper;
import com.siwei.mes.mapper.experiment.ExperimentCheckTypeRecordMapper;
import com.siwei.mes.service.experiment.CheckQuickConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 快检指标配置(CheckQuickConfig)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:22:19
 */
@Slf4j
@Service
public class CheckQuickConfigServiceImpl implements CheckQuickConfigService {
    @Resource
    private CheckQuickConfigMapper checkQuickConfigMapper;
    @Resource
    private ExperimentCheckItemorderMapper experimentCheckItemorderMapper;
    @Resource
    private ExperimentCheckTypeRecordMapper experimentCheckTypeRecordMapper;

    /**
     * 组装指标用于后面通过js去执行
     *
     * @param entity
     */
    private void payloadFormula(CheckQuickConfig entity) {
        /**组装合格指标 用于后面通过js去执行*/
        String qualifiedFormula = ScriptUtils.makeFormula(entity.getQualifiedCheckOneType(), entity.getQualifiedCheckOneValue(), entity.getQualifiedCheckTwoType(), entity.getQualifiedCheckTwoValue());
        entity.setQualifiedCheckText(qualifiedFormula);
        /**组装让步指标  用于后面通过js去执行*/
        String compromiseFormula = ScriptUtils.makeFormula(entity.getCompromiseCheckOneType(), entity.getCompromiseCheckOneValue(), entity.getCompromiseCheckTwoType(), entity.getCompromiseCheckTwoValue());
        entity.setCompromiseCheckText(compromiseFormula);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(CheckQuickConfig entity) {
        List<CheckQuickConfig> list = checkQuickConfigMapper.getList(new CheckQuickConfig().setTestProjectCode(entity.getTestProjectCode()));
        assertTrue(CollectionUtils.isNotEmpty(list), "试验项目已存在");
        this.payloadFormula(entity);
        entity.init(true);
        checkQuickConfigMapper.insertSelective(entity);
        /**只要操作了就清空 2025-03-21 不需要清空了*/
        //log.info("新增快检指标，开始清空【experimentCheckItmeorder】表所有批检关联关系，对应projectCategory：【{}】", entity.getProjectCategory());
        //if (entity.getProjectCategory().equals(MaterialsEnum.CONCRETE.getCode())) {
        //    experimentCheckItemorderMapper.deleteByCheckType(CheckEnum.QUICK.getCode());
        //}
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CheckQuickConfig entity) {
        CheckQuickConfig checkQuickConfig = checkQuickConfigMapper.selectByPrimaryKey(entity.getId());
        assertTrue(checkQuickConfig == null, "快检指标配置信息不存在");
        this.payloadFormula(entity);
        entity.init(false);
        checkQuickConfigMapper.updateByPrimaryKeySelective(entity);

        /**这里修改指标二的信息  可以为null的*/
        checkQuickConfigMapper.updateIsNullInfo(entity);

        /**只要操作了就清空 2025-03-21 不需要清空了*/
        //log.info("修改快检指标，开始清空【experimentCheckItmeorder】表所有批检关联关系，对应projectCategory：【{}】", entity.getProjectCategory());
        //if (entity.getProjectCategory().equals(MaterialsEnum.CONCRETE.getCode())) {
        //    experimentCheckItemorderMapper.deleteByCheckType(CheckEnum.QUICK.getCode());
        //}
        //
        //experimentCheckTypeRecordMapper.deleteByCheckType(CheckEnum.QUICK.getCode(), entity.getProjectCategory());
    }

    @Override
    public void delete(Long id) {
        CheckQuickConfig checkQuickConfig = checkQuickConfigMapper.selectByPrimaryKey(id);
        assertTrue(checkQuickConfig == null, "快检指标配置信息不存在");
        checkQuickConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<CheckQuickConfig> getPage(PageQuery<CheckQuickConfig, CheckQuickConfig> query) {
        CheckQuickConfig searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> checkQuickConfigMapper.getList(searchParam));
    }

    @Override
    public CheckQuickConfig loadById(Long id) {
        return checkQuickConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckQuickConfig> getAll(CheckQuickConfig entity) {
        return checkQuickConfigMapper.getList(entity);
    }

    @Override
    public void updateByPrimaryKeySelective(CheckQuickConfig entity) {
        CheckQuickConfig checkQuickConfig = checkQuickConfigMapper.selectByPrimaryKey(entity.getId());
        assertTrue(checkQuickConfig == null, "快检指标配置信息不存在");
        checkQuickConfigMapper.updateByPrimaryKeySelective(entity.init(false));
    }
}
